package com.jf.ai.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-06-13
 * Time:14:34
 */
@Service
public class ListStructService {
    private final ChatClient chatClient;
    private final ChatModel chatModel;
    public ListStructService(ChatClient.Builder builder, DeepSeekChatModel deepSeekChatModel) {
        this.chatClient = builder.build();
        this.chatModel = deepSeekChatModel;
    }
    public List<String> getListChatClient(String key) {
        List<String> subject = chatClient.prompt()
                .user(u -> u.text("List five {subject}").param("subject", key))
                .call()
                .entity(new ListOutputConverter(new DefaultConversionService()));
        return subject;
    }
    public List<String> getListByChatModel(String key) {
        ListOutputConverter converter = new ListOutputConverter();

        String format = converter.getFormat();

        String template = """
                List five {subject}
                {format}
                """;

        Prompt prompt = PromptTemplate.builder()
                .template(template)
                .variables(Map.of("subject", key, "format", format))
                .build().create();

        Generation generation = chatModel.call(prompt).getResult();

        List<String> list = converter.convert(Objects.requireNonNull(generation.getOutput().getText()));
        return list;
    }
}
